using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public PlayerInputControl inputControl;
    public Vector2 inputDirection;
    public float speed;
    private Rigidbody2D rb;


    private void Awake() {
        inputControl = new PlayerInputControl();

        rb = GetComponent<Rigidbody2D>();
    }

    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    private void Update() {
        inputDirection = inputControl.GamePlayer.Move.ReadValue<Vector2>();
    }

    private void FixedUpdate()
    {
       Move();
    }
    private void Move(){
        // 通过设置x轴的速度，来实现系统
        rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, rb.velocity.y);
        // 通过修改scale x 1 或 -1 来翻转人物，实现移动时人物朝向
        int faceDir = (int) transform.localScale.x;
        if (inputDirection.x >= 0) {
           faceDir = 1;
        }else {
           faceDir = -1;
        }
         transform.localScale = new Vector3(faceDir, 1, 1);
    }
}
